﻿using Coldairarrow.Business.TD;
using Coldairarrow.Entity.DingReving;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.TD;
using Coldairarrow.IBusiness;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.TD
{
    [Route("/TD/[controller]/[action]")]
    public class TD_ReceivingController : BaseApiController
    {
        #region DI

        private readonly RedisOptions _redisOptions; //1.声明实体
        private readonly MongoDBOptions _mongodbOptions;
        public TD_ReceivingController(ITD_ReceivingBusiness tD_ReceivingBus, IOperator op, IOptions<MongoDBOptions> mongodbOptions, IOptions<RedisOptions> redisOptions)
        {
            _tD_ReceivingBus = tD_ReceivingBus;
            _Op = op;
            _mongodbOptions = mongodbOptions.Value;
            _redisOptions = redisOptions.Value;//3.赋值
        }

        ITD_ReceivingBusiness _tD_ReceivingBus { get; }
        IOperator _Op { get; }

        #endregion

        #region 获取


        [HttpPost]
        public async Task<TD_Receiving> GetTheData(IdInputDTO input)
        {
            return await _tD_ReceivingBus.GetTheDataAsync(input.id);
        }
        /// <summary>
        /// 获取收货单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<TD_Receivinginfo>> GetDataList(PageInput<ReceivingConditionDTO> input)
        {
            return await _tD_ReceivingBus.GetDataListinfo(input);
        }
        /// <summary>
        /// 获取收货单的已入库的明细信息
        /// </summary>
        /// <param name="receivingId">收货单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_UpLoadListDataDTO>> GetLocalMaterial_SList(string receivingId)
        {
            return await _tD_ReceivingBus.GetLocalMaterial_SListAsync(receivingId);
        }

        #endregion
        /// <summary>
        /// 提交审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]

        public async Task<AjaxResult> SaveToExamine(QuerServeTOExamineDTO querTable)
        {
            return await _tD_ReceivingBus.SaveToExamine(querTable);
        }
        /// <summary>
        /// 开始作业
        /// </summary>
        /// <returns></returns>
        [HttpPost]

        public async Task<AjaxResult> SaveStartJob(string idIn)
        {
            return await _tD_ReceivingBus.SaveStartJob(idIn);
        }
        #region 提交
        /// <summary>
        /// 打印收货单
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TD_Receivinglistinfo>> GetPrints(string ParentId)
        {
            var res = await _tD_ReceivingBus.GetDataListInfos(ParentId);
            return res;
        }

        [HttpPost]
        public async Task SaveOutData(TD_Receiving data) {
            if (data.Id.IsNullOrEmpty()) {
                InitEntity(data);
                foreach (var item in data.RecDetails) {
                    InitEntity(item);
                    item.RecId = data.Id;
                    item.StorId = data.StorId;
                    item.Amount = item.Price * item.PlanNum;
                    item.TaxRate = item.TaxRate;
                }
                await _tD_ReceivingBus.AddOutDataAsync(data);
            }
            else {
                foreach (var item in data.RecDetails) {
                    if (item.Id.StartsWith("newid_"))
                        InitEntity(item);
                    item.RecId = data.Id;
                    item.StorId = data.StorId;
                    item.Amount = item.Price * item.PlanNum;
                    item.RecNum = item.PlanNum;
                    item.TaxRate = item.TaxRate;
                }
                await _tD_ReceivingBus.UpdateOutDataAsync(data);
            }
        }
        [HttpPost]
        public async Task SaveData(TD_Receiving data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
               // data.StorId = _Op.Property.DefaultStorageId;
                foreach (var item in data.RecDetails)
                {
                    InitEntity(item);
                    item.RecId = data.Id;
                    item.StorId = data.StorId;
                    item.Amount = item.Price * item.PlanNum;
                    item.RecNum = item.PlanNum;
                    item.TaxRate = item.TaxRate;
                }
                await _tD_ReceivingBus.AddDataAsync(data);
            }
            else
            {
                foreach (var item in data.RecDetails)
                {
                    if (item.Id.StartsWith("newid_"))
                        InitEntity(item);
                    item.RecId = data.Id;
                    item.StorId = data.StorId;
                    item.Amount = item.Price * item.PlanNum;
                    item.RecNum = item.PlanNum;
                    item.TaxRate = item.TaxRate;
                }
                await _tD_ReceivingBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _tD_ReceivingBus.DeleteDataAsync(ids);
        }
        /// <summary>
        /// 朱柏清，确认 2021-12-22
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task ConfirmData(List<string> ids)
        {
            AuditDTO audit = new AuditDTO();
            audit.Id = ids[0].ToString();
            audit.AuditType = AuditType.Confirm;
            audit.AuditUserId = _Op.UserId;
            audit.AuditTime = DateTime.Now;
            await _tD_ReceivingBus.Approval(audit);
        }

        /// <summary>
        /// 扫描物料
        /// </summary>
        /// <param name="packqrcode">物料二维码</param>
        /// <param name="recqrcode">收货单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_MaterialRecDetailDTO> GetMaterialRecDetailDataAsync(string packqrcode, string recqrcode)
        {
            return await _tD_ReceivingBus.GetMaterialRecDetailDataAsync(packqrcode, recqrcode);
        }

        /// <summary>
        /// 根据收货单上架入库
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveShelfStorage(WarehousingDTO data)
        {
            await _tD_ReceivingBus.SaveShelfStorageAsync(data, _redisOptions);
        }
      
        [HttpPost]
        public async Task<ReceivePackDetail> GetReceivePackDetail(string packQrcode, string RecId) {
            return await _tD_ReceivingBus.GetReceivePackDetail( packQrcode,  RecId);
        }

       

        /// <summary>
        /// 上架入库复核
        /// </summary>
        /// <param name="receivingId">收货单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitLocalMaterial(string receivingId)
        {
            await _tD_ReceivingBus.SubmitLocalMaterialAsync(receivingId);
        }

        /// <summary>
        /// 上架入库提交
        /// </summary>
        /// <param name="receivingId">收货单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitReceing(string receivingId)
        {
            await _tD_ReceivingBus.SubmitReceingAsync(receivingId);
        }

        /// <summary>
        /// 收货单复核退回改为开始作业状态
        /// </summary>
        /// <param name="receivingId">收货单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpReceReturn(string receivingId)
        {
            await _tD_ReceivingBus.UpReceReturnAsync(receivingId);
        }

        /// <summary>
        /// 删除已入库的包信息
        /// </summary>
        /// <param name="receivingId">收货单id</param>
        /// <param name="data">库存包信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteStockData(string receivingId, PB_UpLoadListDataDTO data)
        {
            await _tD_ReceivingBus.DeleteStockDataAsync(receivingId, data);
        }


        /// <summary>
        /// 收货单打码明细
        /// </summary>
        /// <param name="recid">收货单id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TD_RecDetailPrintDTO>> GetRecDetailPrintList(string recid)
        {
            return await _tD_ReceivingBus.GetRecDetailPrintListAsync(recid);
        }

        /// <summary>
        /// 收货单物料二维码打印
        /// </summary>
        /// <param name="data"></param>
        /// 推送队列增加批次    2022-03-01  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task RecDetailPrintListData(RecDetailPrintDTO data)
        {
            await _tD_ReceivingBus.RecDetailPrintListDataAsync(data, _mongodbOptions, _redisOptions);
        }

        /// <summary>
        /// 收货单物料二维码打印(推送包码和成品码)
        /// </summary>
        /// <param name="data"></param>
        /// <param name="_mongodbOptions"></param>
        /// <param name="_redisOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task RecDetailPrintList(RecDetailPrintDTO data)
        {
            await _tD_ReceivingBus.RecDetailPrintListAsync(data, _mongodbOptions, _redisOptions);
        }

        /// <summary>
        /// 成品物料明细
        /// </summary>
        /// 增加分页逻辑  2022-02-18  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<JobBookingMaterialDTO>> GetRecDetailJobBookingQrCode(PageInput<PB_JobBookingQrCodeConditionDTO> input)
        {
            return await _tD_ReceivingBus.GetRecDetailJobBookingQrCodeAsync(input);
        }

        /// <summary>
        /// 成品物料补打
        /// </summary>
        /// <param name="data"></param>
        /// 推送队列增加批次    2022-03-01  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task PrintRecDetailJobBookingQrCodeAsync(PB_DesignDetailPrintDataDTO data)
        {
            await _tD_ReceivingBus.PrintRecDetailJobBookingQrCodeAsync(data, _mongodbOptions);
        }

        /// <summary>
        /// 成品物料对应的包明细
        /// </summary>
        /// <param name="recid">收货单id</param>
        /// <param name="MaterialId">物料Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_PackListRecDTO>> GetRecPackListData(string recid, string MaterialId)
        {
            return await _tD_ReceivingBus.GetRecPackListDataAsync(recid, MaterialId);
        }

        /// <summary>
        /// 成品物料对应的包补打
        /// </summary>
        /// <param name="data"></param>
        /// <param name="_mongodbOptions"></param>
        /// 推送队列增加批次    2022-03-01  赵锦涛
        /// <returns></returns>
        [HttpPost]
        public async Task PrintRecPackListDataAsync(PB_DesignDetailPrintDataDTO data)
        {
            await _tD_ReceivingBus.PrintRecPackListDataAsync(data, _mongodbOptions);
        }

        /// <summary>
        /// 收货单作废
        /// </summary>
        /// <param name="Id">收货单id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task CancelReceivingDataAsync(string Id)
        {
            await _tD_ReceivingBus.CancelReceivingDataAsync(Id);
        }

        #endregion

        #region 半成品入库

        /// <summary>
        /// 获取半成品信息
        /// 根据流转卡统计完的数量入库
        /// </summary>
        /// <param name="CirBillQrCode">流转卡二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CirBillMaterialDTO> GetCirBillMaterialData(string CirBillQrCode)
        {
            return await _tD_ReceivingBus.GetCirBillMaterialDataAsync(CirBillQrCode);
        }

        /// <summary>
        /// 半成品通知入库
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveCirBillStorDataAsync(CirBillStorData data)
        {
            await _tD_ReceivingBus.SaveCirBillStorDataAsync(data);
        }

        /// <summary>
        /// PDA根据收货单上架入库扫描流转单获取物料信息
        /// </summary>
        /// <param name="CirBillQrCode">流转卡二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CirBillMaterialRecDTO> GetCirBillMaterialRecData(string CirBillQrCode)
        {
            return await _tD_ReceivingBus.GetCirBillMaterialRecDataAsync(CirBillQrCode);
        }

        #endregion

        #region 回货入库打码

        /// <summary>
        /// 回货入库打码
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task PrintRecoveryList(RecoveryMaterialDTO data)
        {
            await _tD_ReceivingBus.PrintRecoveryListAsync(data, _mongodbOptions, _redisOptions);
        }

        #endregion


        #region 待维入库
        /// <summary>
        /// PDA待维入库获取明细数据
        /// </summary>
        /// <param name="receivingId"></param>
        /// <returns></returns>
        public async Task<List<PB_UpLoadListDataDTO>> GetLocalMaterialOldAsync(string receivingId)
        {
            var list = await _tD_ReceivingBus.GetLocalMaterialOldAsync(receivingId);
            return list;
        }

        /// <summary>
        /// PDA待维入库提交
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveRecoveryWarehousingDto(Entity.DingReving.RecoveryWarehousingDto data)
        {
            await _tD_ReceivingBus.SaveRecoveryWarehousingDto(data);
        }

        /// <summary>
        /// PDA扫码收获单获取明细数据
        /// </summary>
        /// <param name="QrCode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TD_RecDetailInfo>> GetTD_ReceivingInfo(string QrCode, string MaterName)
        {
            var list = await _tD_ReceivingBus.GetTD_ReceivingInfo(QrCode, MaterName);
            return list;
        }
        #endregion
    }
}